﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace WikiDoc.Lines
{
    class AlphaListLine : ListLine
    {
        protected AlphaListLine(string line, int level, int level2, string content) 
            : base(line, LineTypes.AlphaList, level, level2, content)
        {
        }

        /// <summary>
        /// 字母列表行定义：[至少一个空格][字母][.、空格][若干空格] 内容
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A (\s+)  ([a-zA-Z] [\.、\s]\s*)  (.*)",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        [CanBeNull]
        public static WikiLine TryParse(string line)
        {
            if (line == null) return null;

            var match = Pattern.Match(line);

            return match.Success
                       ? new AlphaListLine(line,
                                      match.Groups[1].Length,
                                      match.Groups[1].Length + match.Groups[2].Length,
                                      match.Groups[3].Value)
                       : null;
        }
    }
}
